Skip to content

Support protobuf fields.#82

Merged
yue-fred-gao merged 23 commits intosonic-net:masterfrom
dypet:support_protobuf
Jul 31, 2025
Merged

Support protobuf fields.#82
yue-fred-gao merged 23 commits intosonic-net:masterfrom
dypet:support_protobuf

Conversation

@dypet
Copy link
Contributor

@dypet dypet commented Jul 8, 2025

Adding support for protobuf field values in DASH_HA_SET_CONFIG_TABLE and DASH_HA_SCOPE_CONFIG_TABLE

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@@ -207,19 +208,7 @@ pub fn now_in_millis() -> i64 {
#[derive(Serialize, Deserialize, SonicDb)]
#[sonicdb(table_name = "DASH_HA_SET_CONFIG_TABLE", key_separator = ":", db_name = "APPL_DB")]
pub struct DashHaSetConfigTable {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to keep DashHaSetConfigTable structure with a single attribute? Can we move the derive macros to HaSetConfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@dypet dypet requested a review from yue-fred-gao July 11, 2025 17:09
Copy link
Collaborator

@yue-fred-gao yue-fred-gao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good in general. Let me review it when I get back.


let mut dpu_ha_state_state5 = make_dpu_ha_scope_state("active");
dpu_ha_state_state5.ha_term = "2".to_string();
dpu_ha_state_state5.ha_term = "active".to_string();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is ha_term a number?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

scope: dash_ha_set_config.scope.clone(),
vip_v4: dash_ha_set_config.vip_v4.as_ref().map(ip_to_string).unwrap_or_default(),
vip_v6: dash_ha_set_config.vip_v6.as_ref().map(ip_to_string),
owner: format!("{:?}", dash_ha_set_config.owner).into(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need proper conversion. I think the convention is removing prefix and changing to lower case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

use swss_common::SonicDbTable;
use swss_common_testing::*;

fn protobuf_struct_to_json<T: prost::Message + Default + serde::Serialize>(cfg: &T) -> HashMap<String, CxxString> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is _to_json but actually it returns kfv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


// Implements internal action functions for HaScopeActor
impl HaScopeActor {
fn desired_ha_state_to_ha_role(desired_ha_state: i32) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to move this function to sonic-dash-api-proto/src/lib.rs for helper functions around protobuf types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

send! { key: DashHaScopeConfigTable::table_name(), data: { "key": &scope_id, "operation": "Set",
"field_values": {"version": "1", "disable": "true", "desired_ha_state": "active", "approved_pending_operation_ids": "" },
send! { key: HaScopeConfig::table_name(), data: { "key": &scope_id, "operation": "Set",
"field_values": {"json": "{\"version\":\"1\",\"disabled\":true,\"desired_ha_state\":2,\"approved_pending_operation_ids\":[]}"},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use enum value here and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Err(anyhow::anyhow!("DPU entry not found for slot {}", dpu_id))
}

pub fn ip_to_string(ip: &IpAddress) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to sonic-dash-api-proto crate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}
}

pub fn decode_from_json_string<T: for<'de> serde::Deserialize<'de>>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to move decode/encode function for protobuf objects to sonic-dash-api-proto? And the name should be from_field_values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


proto_config.type_attribute(
"dash.ha_scope_config.HaScopeConfig",
"#[allow(unused_imports)] use sonicdb_derive::SonicDb;",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why allow(unused_imports)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

impl HaScopeActor {
fn desired_ha_state_to_ha_role(desired_ha_state: i32) -> String {
match DesiredHaState::try_from(desired_ha_state) {
Ok(DesiredHaState::HaStateActive) => "active".to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the enum name is DesiredHaState not HaState based on protobuf? If HaState is used in another field, will it generate a different enum type in rust?
Can we use strum::Display to generate this function automatically?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see protobuf defines DesiredHaState type

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jing will change the enums to follow proper naming convention: sonic-net/sonic-dash-api#42. With the change, I believe Prost will generate rust enum value without the common prefix. Then you can use strum::Display to generate above mapping.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

true
}

fn convert_pb_to_json(kfv: &mut KeyOpFieldValues) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this function be implemented in the trait instead of generated by macro? If is_proto returns false, it can do nothing as the default implementation. Then you don't fold expanded_proto into expanded to avoid duplicate code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@dypet dypet requested a review from yue-fred-gao July 31, 2025 17:40
@yue-fred-gao yue-fred-gao merged commit cfcc624 into sonic-net:master Jul 31, 2025
3 checks passed
qiluo-msft pushed a commit to sonic-net/sonic-swss-common that referenced this pull request Jul 31, 2025
Moving SonicDbTable to sonic-dash-ha as part of sonic-net/sonic-dash-ha#82
wen587 pushed a commit to wen587/sonic-swss-common that referenced this pull request Aug 5, 2025
@mssonicbld
Copy link

Cherry-pick PR to msft-202506: Azure/sonic-dash-ha.msft#13

mssonicbld added a commit to mssonicbld/sonic-swss-common.msft that referenced this pull request Aug 27, 2025
~~Adding is_proto and convert_pb_to_json to SonicDbTable to support the changes in sonic-net/sonic-dash-ha#82

Moving SonicDbTable to sonic-dash-ha as part of sonic-net/sonic-dash-ha#82
mssonicbld added a commit to Azure/sonic-swss-common.msft that referenced this pull request Aug 27, 2025
~~Adding is_proto and convert_pb_to_json to SonicDbTable to support the changes in sonic-net/sonic-dash-ha#82

Moving SonicDbTable to sonic-dash-ha as part of sonic-net/sonic-dash-ha#82
mssonicbld added a commit to mssonicbld/sonic-swss-common.msft that referenced this pull request Sep 10, 2025
~~Adding is_proto and convert_pb_to_json to SonicDbTable to support the changes in sonic-net/sonic-dash-ha#82

Moving SonicDbTable to sonic-dash-ha as part of sonic-net/sonic-dash-ha#82
mssonicbld added a commit to Azure/sonic-swss-common.msft that referenced this pull request Sep 10, 2025
~~Adding is_proto and convert_pb_to_json to SonicDbTable to support the changes in sonic-net/sonic-dash-ha#82

Moving SonicDbTable to sonic-dash-ha as part of sonic-net/sonic-dash-ha#82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants